package com.exce;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * @Author WaveHi
 * @Date 20/8/2021 下午 9:15
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
public class FileReaderDemoUp {

    public static void main(String[] args) {

        FileReader fr1 = null;
        try{

            //获取数据源
            File destFile1 = new File("day11/data/wps2.txt");

            //获取输入流
            fr1 = new FileReader(destFile1);

            //接收数据
            StringBuilder data = new StringBuilder((int)destFile1.length());

            //接收单次读取的数据
            char[] cbuf = new char[10];

            //接收单次读取的数据的长度
            int len = 0;

            //读取数据
            while((len = fr1.read(cbuf)) != -1) {

                data.append(new String(cbuf,0,len));

            }

            //接收对象的信息
            String[] objects = new String(data).split(";");

            //接收对象
            Student[] students = new Student[objects.length];

            //标记对象的存储位置
            int index = 0;

            //循环获取对象
            for(String ele : objects) {

                String[] attributes = ele.split("-");
                students[index] = new Student(attributes[0],Integer.valueOf(attributes[1]),Integer.valueOf(attributes[2]));
                index++;

            }

            //接收总分
            int sum = 0;

            //循环统计总分
            for(Student s : students) {

                sum += s.getScore();

            }

            //输出结果
            System.out.println("Sum is : " + sum);

        }catch(IOException e) {

            e.printStackTrace();

        }finally {

            //资源的关闭
            if(fr1 != null) {

                try{

                    fr1.close();

                }catch(IOException e) {

                    e.printStackTrace();

                }

            }

        }

    }

}

//学生类
class Student {

    private String name;//学生姓名
    private int age;//学生年龄
    private int score;//学生成绩

    public Student() {

    }

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}
